package com.lvyq.controller;

import com.lvyq.cache.CacheManager;
import com.lvyq.common.MsgTypeConstant;
import com.lvyq.model.ai.Corpus;
import com.lvyq.model.receive.ReceiveMsg;
import com.lvyq.model.reply.ReplyMsg;
import com.lvyq.service.OrderService;
import com.lvyq.service.ReceiveMsgService;
import com.lvyq.service.UserService;
import com.lvyq.util.TokenCheckUtils;
import com.lvyq.util.XMLUtils;
import com.lvyq.wx.server.WeChatTokenCheckServer;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Random;

/**
 * @program: weg
 * @description: 微信公众号平台token验证
 * @author: lvyq
 * @create: 2023-03-07 14:49
 **/
@RequestMapping
@Controller
@CrossOrigin
@Log
public class TokenCheckController {

    @Value("${wechat.token}")
    public String token;

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    @Resource
    private ReceiveMsgService receiveMsgService;

    @RequestMapping("/")
    public void index(HttpServletResponse response, HttpServletRequest request) throws Exception{
        boolean state= WeChatTokenCheckServer.checkToken(response,request,token);
        if (state && request.getMethod().equals("POST")){
           ReceiveMsg receiveMsg = XMLUtils.XMLTOModel(request);
            receiveMsgService.save(receiveMsg);
           //用户存储
           userService.saveUser(receiveMsg.getFromUserName());
           //指令回复
            orderService.orderReply(response,receiveMsg);
        }else {
            log.info("请求无效");
        }
    }





}
